package com.thermal.thermal_inspection.util;


import com.thermal.thermal_inspection.entity.Result;
import com.thermal.thermal_inspection.entity.User;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import com.thermal.thermal_inspection.vo.ResultRequestVo;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class ResultUtil {
    public String generateFileName(String patientName, String doctorName) {
        String dateStr = new SimpleDateFormat("yyyyMMdd_HHmmssSSS").format(new Date());
        return patientName + "_" + doctorName + "_" + dateStr + ".pdf";
    }

    public boolean generatePdf(ResultRequestVo result, User patient, User doctor, String filePath) {
        try {
            Document document = new Document(PageSize.A4);
            PdfWriter.getInstance(document, new FileOutputStream(filePath));
            document.open();

            // 字体设置（使用微软雅黑，支持中文）
            InputStream fontStream = this.getClass().getClassLoader().getResourceAsStream("fonts/msyh.ttf");
            if (fontStream == null) {
                throw new RuntimeException("字体文件未找到，请确保路径为 resources/fonts/msyh.ttf");
            }
            byte[] fontBytes = fontStream.readAllBytes();
            BaseFont baseFont = BaseFont.createFont("msyh.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, fontBytes, null);
            Font titleFont = new Font(baseFont, 18, Font.BOLD);
            Font normalFont = new Font(baseFont, 12);

            // 标题
            document.add(new Paragraph("临床诊断报告", titleFont));
            document.add(new Paragraph(" "));

            // 病人基本信息
            document.add(new Paragraph("【病人信息】", normalFont));
            document.add(new Paragraph("姓名：" + patient.getRealName(), normalFont));
            document.add(new Paragraph("性别：" + patient.getGender(), normalFont));
            document.add(new Paragraph("年龄：" + patient.getAge(), normalFont));
            document.add(new Paragraph("电话：" + patient.getPhone(), normalFont));
            document.add(new Paragraph("住址：" + patient.getDetailedAddress(), normalFont));
            document.add(new Paragraph("职业：" + patient.getOccupation(), normalFont));
            document.add(new Paragraph("药物过敏史：" + patient.getAllergyHistory(), normalFont));
            document.add(new Paragraph("自述信息：" + patient.getSelfDescription(), normalFont));
            document.add(new Paragraph(" "));

            // 诊断信息
            document.add(new Paragraph("【初步诊断】", normalFont));
            document.add(new Paragraph("诊断类型：" + result.getDiagnosisType(), normalFont));
            document.add(new Paragraph("诊断时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()), normalFont));
            document.add(new Paragraph("诊断内容：" + result.getResultContent(), normalFont));
            document.add(new Paragraph(" "));

            // 医生信息
            document.add(new Paragraph("【医生信息】", normalFont));
            document.add(new Paragraph("姓名：" + doctor.getRealName(), normalFont));
            document.add(new Paragraph("科室/单位：" + doctor.getWorkplace(), normalFont));
            document.add(new Paragraph("联系电话：" + doctor.getPhone(), normalFont));
            document.add(new Paragraph(" "));

            // 核心内容
            document.add(new Paragraph("【诊断依据】", normalFont));
            document.add(new Paragraph(result.getDiagnosticBasis(), normalFont));
            document.add(new Paragraph(" "));

            document.add(new Paragraph("【鉴别诊断】", normalFont));
            document.add(new Paragraph(result.getDifferentialDiagnosis(), normalFont));
            document.add(new Paragraph(" "));

            document.add(new Paragraph("【治疗计划】", normalFont));
            document.add(new Paragraph(result.getTreatmentPlan(), normalFont));

            document.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
